<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the Request class.
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Request
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Request.php 41 2008-08-30 16:11:11Z mjijackson $
 */

/**
 * Automatically standardizes request input and validates request level
 * keys. Also provides convenient accessor methods for the superglobal
 * $_REQUEST array.
 *
 * @package     Request
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class Request
{

    /**
     * Constructor. Validates request keys and standardizes new lines in
     * request data.
     *
     * @access  private
     */
    private function __construct()
    {
        foreach ($_REQUEST as $key => $val) {
            $this->validateKey($key);
            $_REQUEST[$key] = $this->cleanData($val);
        }
    }

    /**
     * Gets the instance of the Request class.
     *
     * @return  Request         The Request singleton object
     * @access  public
     * @static
     */
    public static function getInstance()
    {
        static $inst;
        if (!$inst) {
            $inst = new self();
        }
        return $inst;
    }

    /**
     * Gets a variable from the request.
     *
     * @param   string  $key    The name of the request variable
     * @return  mixed           The value of the request variable
     * @access  public
     */
    public function get($key)
    {
        return isset($_REQUEST[$key]) ? $_REQUEST[$key] : null;
    }

    /**
     * Sets a variable in the request to the given value.
     *
     * @param   string  $key    The name of the request variable
     * @param   mixed   $value  The value of the request variable
     * @return  void
     * @access  public
     */
    public function set($key, $value)
    {
        $_REQUEST[$key] = $value;
    }

    /**
     * Makes sure that a key uses only alpha-numeric text, underscores, hyphens,
     * backslashes, and colons. If an invalid input key is found, the script
     * will immediately halt execution and an error will be displayed.
     *
     * @param   string  $key    The input key to validate
     * @return  void
     * @access  private
     */
    private function validateKey($key)
    {
        if (!preg_match('/^[a-z0-9:_\/-]+$/i', $key)) {
             die("Invalid request key: $key");
        }
    }

    /**
     * Strips slashes (if magic quotes are turned on) and standardizes newline
     * characters to \n. Works recursively on nested data (arrays).
     *
     * @param   mixed   $data   The data to clean
     * @return  mixed           The sanitized data
     * @access  private
     */
    private function cleanData($data)
    {
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                $this->validateKey($key);
                $data[$key] = $this->cleanData($value);
            }
            return $data;
        }

        // strip slashes if magic quotes are on
        if (get_magic_quotes_gpc()) {
            $data = stripslashes($data);
        }

        // standardize newlines
        return str_replace(array("\r\n", "\r"), "\n", $data);
    }

}

?>
